package prbase;

import java.awt.Desktop;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Vars {

    // Version = major.minor-build
    private static String Version = "Производственная база v0.93-02b";

    public static String getVersion() {
        return Version;
    }
    // Properties
    /*
     * Username - database user name;
     * Userpass - database user password;
     * DB_URL - url for connecting to database.
     * Filestore - path to filestore.
     */
    public static Properties prop = new Properties();
    public static String PropFileName = "Options.xml";

    public static void SaveProperties() {
        try {
            prop.storeToXML(new FileOutputStream(PropFileName, false), null);
        } catch (IOException ex) {
            System.out.println("Vars.SaveProperties(): " + ex.toString());
        }
    }

    public static void LoadProperties() {
        try {
            Vars.prop.loadFromXML(new FileInputStream(PropFileName));
        } catch (IOException ex) {
            System.out.println("Vars.LoadProperties(): " + ex.toString());
        }
    }

    public static String getHexString(String str) {
        String hexstr = "";
        int i = 0;
        while (i < str.length()) {
            Integer ic = str.codePointAt(i);
            hexstr = hexstr + Integer.toHexString(ic);
            hexstr = hexstr + "|";
            i++;
        }
        return hexstr;
    }

    public static String getStringFromHex(String hexstr) {
        if (hexstr == null) {
            return "";
        }
        String str = "";
        String s = "";
        Integer ci = 0;
        char c = (char) 0;
        int i = 0;
        while (i < hexstr.length()) {
            if (!hexstr.substring(i, i + 1).equals("|")) {
                s = s + hexstr.substring(i, i + 1);
                i++;
            } else {
                try {
                    ci = Integer.parseInt(s, 16);
                } catch (NumberFormatException ex) {
                    Logger.getLogger(Vars.class.getName()).log(Level.SEVERE, null, ex);
                    return "Error parsing HEX";
                }
                c = (char) ci.intValue();
                str = str + c;
                s = "";
                i++;
            }
        }
        return str;
    }

    public static String getFileNameFromString(String path) {
        if (path.length() == 0) {
            return "";
        }
        int i = path.length() - 1;
        while (i >= 0) {
            if (path.charAt(i) == '/' || path.charAt(i) == '\\') {
                break;
            }
            i--;
        }
        return path.substring(i);
    }

    public static Boolean CopyFile(String SourceFile, String DestFile) {
        FileChannel ic = null;
        try {
            ic = new FileInputStream(SourceFile).getChannel();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Vars.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        FileChannel oc = null;
        try {
            oc = new FileOutputStream(DestFile).getChannel();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Vars.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        try {
            ic.transferTo(0, ic.size(), oc);
            ic.close();
            oc.close();
        } catch (IOException ex) {
            Logger.getLogger(Vars.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return true;
    }

    public static Boolean CopyFileTo(String SourceFileName, obj_Project prg) {



        return true;
    }

    public static String getDocumentPath(Object DocRelObj) {

        

        return "";
    }

    public static Boolean OpenFile(String filename) {
        filename = filename.replace("\\", File.separator);
        filename = filename.replace("/", File.separator);
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.contains("windows")) {
            try {
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler \"" + filename + "\"");
            } catch (IOException ex) {
                Logger.getLogger(Vars.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        } else {
            Desktop dt = Desktop.getDesktop();
            try {
                dt.open(new File(filename));
            } catch (IOException ex) {
                Logger.getLogger(Vars.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }
        return true;
    }
}
